package com.ideal.manage.esms.controller.monitor;

import com.ideal.manage.esms.bean.project.*;
import com.ideal.manage.esms.service.monitor.MonitorHistoryDataPcsService;
import com.ideal.manage.esms.service.monitor.MonitorService;
import com.ideal.manage.esms.service.project.AlarmInfoService;
import com.ideal.manage.esms.service.project.EquipmentManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by 83730 on 2018/5/25.
 */
@Controller
@RequestMapping("monitor")
public class MonitorForPCSController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    @Resource
    private EquipmentManageService equipmentManageService;
    @Resource
    private AlarmInfoService alarmInfoService;

    @Resource
    private MonitorHistoryDataPcsService monitorHistoryDataPcsService;

    /**
     * 系统监控内嵌页面 - 变流器
     *
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_PCS")
    public void monitorWebForPCS(Model model, String id) {
        // 1. 实体类
        EquipmentManage equipmentManage = equipmentManageService.findOne(id);
        model.addAttribute("equipmentManage", equipmentManage);
        if (StringUtils.isBlank(equipmentManage.getCode())) {
            model.addAttribute("message", "请在工程配置->对象管理->设备管理页面设置设备唯一标识，否则无法显示");
            return;
        }
        // 4. 台账信息
        List<AccountProperty> accountPropertyListForPCS = monitorService.accountPropertyList(equipmentManage, 3, null);  // 电池包信息
        model.addAttribute("accountPropertyListForPCS", accountPropertyListForPCS);
        // 2. 实时数据
//        List<ParameterInfo> parameterInfoList = monitorService.findListByTypeForMonitor(equipmentManage.getEquipmentType(),equipmentManage);
        List<ParameterInfo> parameterInfoList = monitorHistoryDataPcsService.findListByTypeForPcsMonitor(equipmentManage.getEquipmentType(),equipmentManage);
        model.addAttribute("parameterInfoList", parameterInfoList);
        // 3. 设备状态
        List<EquipmentInfo> equipmentInfoList= monitorService.findEquipmentListByTypeForMointor(equipmentManage.getEquipmentType(), equipmentManage);
        model.addAttribute("equipmentInfoList",equipmentInfoList);
        //实时报警
        List<AlarmInfo> alarmInfos=alarmInfoService.findAlarmInfosByObjName(equipmentManage.getEquipmentType(),id);
        model.addAttribute("alarmInfos",alarmInfos);

    }

    /**
     * 历史数据
     * @param id        设备id
     * @param objType   测点类别
     * @param name      测点名称
     * @param ranges    时间范围
     * @param freque    显示密度
     * @param show_type 显示类别
     * @param start_time    开始时间
     * @param end_time      结束时间
     * @return
     */
    @RequestMapping("history_data_equipment_pcs")
    @ResponseBody
    public Map historyDataEquipmentPcs(String id, int objType, String name, String ranges,
                                    String freque, String show_type, String start_time, String end_time) {
        if ("custom".equals(ranges) && StringUtils.isBlank(start_time) && StringUtils.isBlank(end_time)){
            return null;
        }
        return monitorHistoryDataPcsService.historyDataEquipmentPcs(id, objType, name, ranges, freque, show_type, start_time, end_time);
    }
}
